package com.example.phonestore.controller;

import com.example.phonestore.entity.Cart;
import com.example.phonestore.entity.showCart;
import com.example.phonestore.mapper.ProductMapper;
import com.example.phonestore.service.CartService;
import com.example.phonestore.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;


    @RequestMapping("/insertCart")
    public JsonResult insertCart( HttpSession session,Cart cart){
        cart.setUid(getuidFromSession(session));
        return cartService.insertCart(cart);
    }

    @RequestMapping({"","/"})
    public List<showCart> showCart(HttpSession session){
        List<showCart> data=cartService.getShowCartByUid(getuidFromSession(session));
        return data;
    }

    @RequestMapping({"/{cid}/addNum"})
    public Integer addNum(@PathVariable("cid") Integer cid, HttpSession session){
        System.out.println(cid);
        Integer num=cartService.updateCartNum(cid,getuidFromSession(session));
        return num;
    }

    @RequestMapping("/{cid}/deleteOne")
    public JsonResult deleteOne(@PathVariable("cid") Integer cid){
        return cartService.deleteCartOne(cid);
    }


    @RequestMapping({"/{cid}/reduceNum"})
    public Integer reduceNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer num=cartService.reduceCartNum(cid,getuidFromSession(session));
        return num;
    }
}
